//
//  RootViewController.m
//  DNDzgz
//
//  Created by gimenete on 27/04/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "RootViewController.h"
//#import "SkyHook.h"

@implementation RootViewController
@synthesize flipInfo;

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	//SkyHook *sky = [[SkyHook alloc] init];
	//[sky getCoordenatesIpodTouch];
	
	services = [[NSDictionary alloc] initWithObjectsAndKeys:@"Autobuses", @"bus", @"Bizi", @"bizi", @"WiFi", @"wifi", nil];
	
	[logo setImage:[UIImage imageNamed:@"cabecera.png"]];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
	UIBarButtonItem *barbuttonitem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
	
	[infoButton addTarget:self
				   action:@selector(showInfo)
		 forControlEvents:UIControlEventTouchUpInside];
	
	self.flipInfo = barbuttonitem;
	self.navigationItem.rightBarButtonItem = flipInfo;
	[barbuttonitem release];
	
	self.tableView.backgroundColor = [UIColor blackColor];
	self.title = @"Inicio";
	
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
	{

		[self.tableView setBackgroundView:nil];
		
		[self.tableView setBackgroundView:[[[UIView alloc] init] autorelease]];
		[self.tableView setFrame:CGRectMake(self.tableView.bounds.origin.x, self.tableView.bounds.origin.y, [UIScreen mainScreen].bounds.size.width, 300)];
		
	} 	

#endif 
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:YES];
	
	// set the frame size
	//CGRect frame = CGRectMake(self.tableView.bounds.origin.x, self.tableView.bounds.origin.x, [UIScreen mainScreen].bounds.size.width, 300);

	//self.tableView.frame = frame;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{	
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
	
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
	{
        return 80;
    } 	
    else 
	{
		return 55;
	}

#endif
}

- (void)showInfo
{    	
	FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
	controller.delegate = self;
	
	controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:controller animated:YES];
	
	[controller release];
}

/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */
/*
 - (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 }
 */
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller 
{    
	[self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning 
{
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload 
{
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	if (section == 0) {
		return [services count];
	}
	return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) 
	{
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    
	if (indexPath.section == 0) 
	{
		cell.textLabel.text = [[services allValues] objectAtIndex:indexPath.row];
		cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ico_home_%@.png", [[services allKeys] objectAtIndex:indexPath.row]]];
	}
	else
	{
		cell.textLabel.text = @"Favoritos";
		cell.imageView.image = [UIImage imageNamed:@"ico_home_favoritos.png"];
	}
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
	{
		cell.textLabel.font = [UIFont boldSystemFontOfSize:30];
    } 	
	
#endif 
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (indexPath.section == 1)
	{
		Favorites *favs = [[Favorites alloc] init];
		[self.navigationController pushViewController:favs animated:YES];
		[favs release];
	}
	else 
	{
		MapViewController *map = [[MapViewController alloc] initWithService:[[services allKeys] objectAtIndex:indexPath.row]];
		
		[map setTitle:[[services allValues] objectAtIndex:indexPath.row]];
		[self.navigationController pushViewController:map animated:YES];
		[map release];
	}
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

- (void)dealloc 
{
	[services release];
	[super dealloc];
}

@end